<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Markdown Editor</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
        }

        #markdown {
            width: 50%;
            height: 80vh;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            padding: 10px;
            outline: none;
        }

        #preview {
            width: 50%;
            height: 80vh;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            padding: 10px;
            overflow-y: scroll;
        }

        #submit {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin-top: 10px;
            cursor: pointer;
        }

        #submit:hover {
            background-color: #3e8e41;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/showdown/dist/showdown.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/showdown/dist/github.min.css">
</head>
<body>
    <p>{{news_html_tag.title}}</p>
    <p>{{news_html_tag.news_type}}</p>
    <img src={{news_html_tag.title_img}}>
    <div id="preview">
        {{news_html_tag.content | safe}}
    </div>

    <script>
        var markdown = document.querySelector('#markdown');
        var preview = document.querySelector('#preview');
        var submit = document.querySelector('#submit');

        var converter = new showdown.Converter();

        markdown.addEventListener('input', function() {
            var markdownText = markdown.value;
            var htmlText = converter.makeHtml(markdownText);
            preview.innerHTML = htmlText;
        });

        submit.addEventListener('click', function() {
            var markdownText = markdown.value;
            var htmlText = converter.makeHtml(markdownText);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://127.0.0.1:8000/edit');
            xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        console.log(xhr.responseText);
                    } else {
                        console.log('Error sending request!');
                    }
                }
            };
            xhr.send(JSON.stringify({html: htmlText}));
        });
    </script>
</body>
</html>
